package com.ml.loan.common.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Calendar;
import java.util.Date;


/**
 * @Author syh
 * @Description LocalDateTimeUtils
 * @Date 11:30 2021/9/8
 * @Param
 * @return
 **/
public class LocalDateTimeUtils {
    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * Date转换为LocalDateTime
     *
     * @param date
     * @return
     */
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime转换为Date
     *
     * @param time
     * @return
     */
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }


    /**
     * 获取指定日期的毫秒
     *
     * @param time
     * @return
     */
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取指定日期的秒
     *
     * @param time
     * @return
     */
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 获取指定时间的指定格式
     *
     * @param time
     * @param pattern
     * @return
     */
    public static String formatTime(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取指定时间的指定格式
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String formatTime(Date date, String pattern) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    /**
     * 获取当前时间的指定格式
     *
     * @param pattern
     * @return
     */
    public static String formatNow(String pattern) {
        return formatTime(LocalDateTime.now(), pattern);
    }

    /**
     * 日期加上一个数,根据field不同加不同值,field为ChronoUnit.*
     *
     * @param time
     * @param number
     * @param field
     * @return
     */
    public static LocalDateTime plus(LocalDateTime time, long number, TemporalUnit field) {
        return time.plus(number, field);
    }

    /**
     * 日期减去一个数,根据field不同减不同值,field参数为ChronoUnit.*
     *
     * @param time
     * @param number
     * @param field
     * @return
     */
    public static LocalDateTime minu(LocalDateTime time, long number, TemporalUnit field) {
        return time.minus(number, field);
    }

    /**
     * 获取两个日期的差  field参数为ChronoUnit.*
     *
     * @param startTime
     * @param endTime
     * @param field     单位(年月日时分秒)
     * @return
     */
    public static long betweenTwoTime(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit field) {
        Period period = Period.between(LocalDate.from(startTime), LocalDate.from(endTime));
        if (field == ChronoUnit.YEARS) return period.getYears();
        if (field == ChronoUnit.MONTHS) return period.getYears() * 12 + period.getMonths();
        return field.between(startTime, endTime);
    }

    /**
     * 获取一天的开始时间，2017,7,22 00:00
     *
     * @param time
     * @return
     */
    public static LocalDateTime getDayStart(LocalDateTime time) {
        return time.withHour(0)
                .withMinute(0)
                .withSecond(0)
                .withNano(0);
    }

    /**
     * 获取一天的结束时间，2017,7,22 23:59:59.999999999
     *
     * @param time
     * @return
     */
    public static LocalDateTime getDayEnd(LocalDateTime time) {
        return time.withHour(23)
                .withMinute(59)
                .withSecond(59)
                .withNano(999999999);
    }

    /**
     * 获取num之前/之后的时间
     * 负数：前
     * 正数：后
     *
     * @param num
     * @param unit 单位：Calendar.SECOND ...
     * @return
     */
    public static String intervalTime(int num, int unit, String format) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(unit, num);
        Date beforeDate = beforeTime.getTime();
        LocalDateTime ldt = beforeDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return ldt.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 获取给定日期的前/后指定天数日期时间
     *
     * @param dateTime 给定的日期
     * @param num      往前(负整数)/往后(正整数)的
     * @param unit     单位：Calendar.SECOND ...
     * @return 、
     */
    public static String someNextTime(LocalDateTime dateTime, Integer num, int unit, String format) {
        Calendar calendar = Calendar.getInstance();
        Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        calendar.setTime(date);
        calendar.add(unit, num);
        Date beforeDate = calendar.getTime();
        LocalDateTime ldt = beforeDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        return ldt.format(DateTimeFormatter.ofPattern(format));
    }

    /**
     * 字符串转日期
     *
     * @param timeStr
     * @return
     */
    public static Date strToDateTime(String timeStr) {
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.parse(timeStr, DateTimeFormatter.ofPattern(PATTERN));
        Instant instant = localDateTime.atZone(zone).toInstant();
        return Date.from(instant);
    }

    /**
     * 获取当前时间大于dateTime的差值
     *
     * @param dateTime
     * @return ms
     */
    public static long getDatePoor(LocalDateTime dateTime) {
        Date date1 = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
        long t1 = date1.getTime();
        Date date2 = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        long t2 = date2.getTime();
        return t2 - t1;
    }

    public static void main(String[] args) {
        long l = betweenTwoTime(LocalDateTime.now().plusDays(-3).minusHours(5), LocalDateTime.now(), ChronoUnit.DAYS);
        System.out.println(l);
    }

    public static long pastMinutes(LocalDateTime dateTime){
        Duration duration = Duration.between(dateTime,LocalDateTime.now());
        return duration.toMinutes();
    }

}
